﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Management;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace Finder.util
{
    public class Comm
    {
        public static void VisiteUrl(string url)
        {
            System.Diagnostics.Process.Start(url);
        }
        
        //返回描述本地计算机上的网络接口的对象(网络接口也称为网络适配器)。
        public static NetworkInterface[] NetCardInfo()
        {
          return NetworkInterface.GetAllNetworkInterfaces();
        }

        ///<summary>
        /// 通过NetworkInterface读取网卡Mac。
        /// 1）如果当前的网卡是禁用状态（硬件处于硬关闭状态），取不到该网卡的MAC地址，（您可以通过禁用网卡进行试验）。
        /// 2）如果当前启用了多个网卡，最先返回的地址是最近启用的网络连接的信息
        ///</summary>
        ///<returns></returns>
        public static List<string> GetMac()
        {
            List<string> macs = new List<string>();
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in interfaces)
            {
                macs.Add(ni.GetPhysicalAddress().ToString());
            }
            return macs;
        }

        /// <summary>
        /// 获取cpuid。此方法借用WMI服务，需要服务器开启WMI服务。否则无法获取。
        /// </summary>
        /// <returns></returns>
        public static string GetCpu()
        {
            string cpuInfo = "";
            ManagementClass cimobject = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = cimobject.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
            }
            return cpuInfo;
        }
        /// <summary>
        /// 获取硬盘ID。此方法借用WMI服务，需要服务器开启WMI服务。否则无法获取。
        /// </summary>
        /// <returns></returns>
        public static string GetHd()
        {
            String HDid = "";
            ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();
            foreach (ManagementObject mo in moc1)
            {
                HDid = (string)mo.Properties["Model"].Value;
            }
            HDid = HDid.Substring(HDid.IndexOf(" ") + 1, 5);
            return HDid;
        }

        /// <summary>
        /// 获取本机外网IP地址信息，包含所在城市
        /// </summary>
        /// <returns></returns>
        public static string GetIpInfo()
        {
            string strUrl = "http://iframe.ip138.com/ic.asp";
            Uri uri = new Uri(strUrl);
            WebRequest wr = WebRequest.Create(uri);
            Stream s = wr.GetResponse().GetResponseStream();
            StreamReader sr = new StreamReader(s, Encoding.Default);
            string all = sr.ReadToEnd();
            all = NoHTML(all);
            return all;
        }
        /// <summary>
        /// 获取本机外网IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetIp()
        {
            string ipInfo = GetIpInfo();
            int i = ipInfo.IndexOf("[") + 1;
            string tempip = ipInfo.Substring(i, 15);
            string ip = tempip.Replace("]", "").Replace(" ", "");
            return ip;
        }

        /// <summary> 
        /// 去除html标签
        /// </summary>  
        /// <param name="Htmlstring"></param> 
        /// <returns></returns>  
        public static string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML 
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);

            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            Htmlstring = Htmlstring.Trim();
            return Htmlstring;
        }
    }
}
